
package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.DownloadsModRequest;

public class GenericDownloadModRequestDAO extends AutoKeys implements
		org.jetnuke.dao.DownloadModRequestDAO {

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadModRequestModel#retrieveByPk(int)
	 */
	public DownloadsModRequest retrieveByPk(int id)throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadModRequestModel.selectByID"));
		p.setInt(1, id);
		
		ResultSet rs = p.executeQuery();
		DownloadsModRequest s = new DownloadsModRequest();
		
		if (rs.next()) {
			fillModRequestFromResultSet(s, rs);
		}
		
		rs.close();
		p.close();

		return s;
	}

	/**
	 * @param s
	 * @param rs
	 */
	private void fillModRequestFromResultSet(DownloadsModRequest s, ResultSet rs)throws Exception {
		s.setBROKENDOWNLOAD(rs.getInt("BROKENDOWNLOAD"));
		s.setCID(rs.getInt("CID"));
		s.setDESCRIPTION(rs.getString("DESCRIPTION"));
		s.setEMAIL(rs.getString("EMAIL"));
		s.setFILESIZE(rs.getInt("FILESIZE"));
		s.setHOMEPAGE(rs.getString("HOMEPAGE"));
		s.setLID(rs.getInt("LID"));
		s.setMODIFYSUBMITTER(rs.getString("MODIFYSUBMITTER"));
		s.setNAME(rs.getString("NAME"));
		s.setREQUESTID(rs.getInt("REQUESTID"));
		s.setSID(rs.getInt("SID"));
		s.setTITLE(rs.getString("TITLE"));
		s.setURL(rs.getString("URL"));
		s.setVERSION(rs.getString("VERSION"));
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadModRequestModel#countModified()
	 */
	public int countModified() throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadModRequestModel.countModified"));
		
		ResultSet rs = p.executeQuery();
		rs.next();
		int r= rs.getInt(1);
		rs.close();
		p.close();
		return r;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadModRequestModel#countBroken()
	 */
	public int countBroken() throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadModRequestModel.countBroken"));
		
		ResultSet rs = p.executeQuery();
		rs.next();
		int r= rs.getInt(1);
		rs.close();
		p.close();
		return r;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadModRequestModel#update(org.jetnuke.entities.DownloadsModRequest)
	 */
	public  int addNew(DownloadsModRequest modRequest) throws Exception {
		
		PreparedStatement p = this.getStatementForAutoKeys("DownloadModRequestModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("DownloadModRequestModel.lastGeneratedDownloadModRequestID"));
		initNewDownload(modRequest,p);
		
		int id = this.executeAutoKeysQuery(p);
		p.close();		
		return id;
		
	}
	
	private void initNewDownload(DownloadsModRequest dc,PreparedStatement p)throws Exception{
		
		p.setInt(1,dc.getBROKENDOWNLOAD());
		p.setInt(2,dc.getCID());
		p.setString(3,dc.getDESCRIPTION());
		p.setString(4,dc.getEMAIL());
		p.setInt(5,dc.getFILESIZE());
		p.setString(6,dc.getHOMEPAGE());
		p.setInt(7,dc.getLID());
		p.setString(8,dc.getMODIFYSUBMITTER());
		p.setString(9,dc.getNAME());
		p.setInt(10,dc.getSID());
		p.setString(11,dc.getTITLE());
		p.setString(12,dc.getURL());
		p.setString(13,dc.getVERSION());
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadModRequestModel#selectBrokenByLID(int)
	 */
	public List selectBrokenByLID(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadModRequestModel.selectBrokenByLID"));
		p.setInt(1,id);	
			
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	private List processSelectAll(ResultSet rs) throws Exception{
		List list = new ArrayList();
		while (rs.next()) {
			DownloadsModRequest u = new DownloadsModRequest();
			fillModRequestFromResultSet(u,rs);			
			list.add(u);
		}
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadModRequestModel#deleteByLid(int)
	 */
	public void deleteByID(int id) throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadModRequestModel.deleteByID"));
		p.setInt(1,id);
		p.executeUpdate();
		p.close();
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadModRequestModel#selectModified()
	 */
	public List selectModified() throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadModRequestModel.selectModified"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadModRequestModel#selectBroken()
	 */
	public List selectBroken() throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadModRequestModel.selectBroken"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	public void deleteByLID(int lid) throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadModRequestModel.deleteByLID"));
		p.setInt(1,lid);
		p.executeUpdate();
		p.close();
		
	}
}
